package com.da.lon.wang.xlg.util;

import android.animation.Animator;
import android.animation.Animator.AnimatorListener;
import android.animation.Keyframe;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.view.View;
import android.widget.ImageView;

public class AnimationUtil {
    /**
     * 开始播放动画
     *
     * @param imageView 动画图片
     * @param points    抛物线的三个点的坐标(x,y)
     */
    public static void startAnimation(final ImageView imageView,
                                      float[][] points, final View addcartButton) {
        try {
            float[] value = calculate(points);
            int count = (int) (points[2][0] - points[0][0]);
            float[] positions = new float[]{value[0], value[1], value[2]};
            Keyframe[] keyframes = new Keyframe[count];
            final float keyStep = 1f / (float) count;
            float key = keyStep;
            for (int i = (int) points[2][0]; i > points[0][0]; --i) {
                keyframes[(int) points[2][0] - i] = Keyframe.ofFloat(key, i + 1);
                key += keyStep;
            }
            PropertyValuesHolder pvhX = PropertyValuesHolder.ofKeyframe(
                    "translationX", keyframes);

            key = keyStep;
            for (int i = (int) points[0][0]; i < (int) points[2][0]; ++i) {
                keyframes[i - (int) points[0][0]] = Keyframe.ofFloat(key, -getY(i + 1, positions));
                key += keyStep;
            }
            PropertyValuesHolder pvhY = PropertyValuesHolder.ofKeyframe(
                    "translationY", keyframes);
            ObjectAnimator yxBouncer = ObjectAnimator.ofPropertyValuesHolder(
                    imageView, pvhY, pvhX).setDuration(800);
            yxBouncer.addListener(new AnimatorListener() {

                @Override
                public void onAnimationStart(Animator animation) {
                    imageView.setVisibility(View.VISIBLE);
                    addcartButton.setEnabled(false);
                }

                @Override
                public void onAnimationRepeat(Animator animation) {
                    addcartButton.setEnabled(true);
                }

                @Override
                public void onAnimationEnd(Animator animation) {
                    imageView.setVisibility(View.GONE);
                    addcartButton.setEnabled(true);
                }

                @Override
                public void onAnimationCancel(Animator animation) {
                    addcartButton.setEnabled(true);
                }
            });
            yxBouncer.start();
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }

    }

    /**
     * 这里是根据三个坐标点计算出来的抛物线方程
     *
     * @param x
     * @return
     */
    private static float getY(float x, float[] positions) {
        return positions[0] * x * x + positions[1] * x + positions[2];
    }

    /**
     * 根据传入的二维数组计算抛物线的三个点
     *
     * @param points
     * @return
     */
    private static float[] calculate(float[][] points) {
        float x1 = points[0][0];
        float y1 = points[0][1];
        float x2 = points[1][0];
        float y2 = points[1][1];
        float x3 = points[2][0];
        float y3 = points[2][1];

        final float a = (y1 * (x2 - x3) + y2 * (x3 - x1) + y3 * (x1 - x2))
                / (x1 * x1 * (x2 - x3) + x2 * x2 * (x3 - x1) + x3 * x3
                * (x1 - x2));
        final float b = (y1 - y2) / (x1 - x2) - a * (x1 + x2);
        final float c = y1 - (x1 * x1) * a - x1 * b;
        return new float[]{a, b, c};
    }

}
